module MetaDataAnalyzer where

import FlsParser
import Data.List(foldr)
import qualified Data.ByteString as B
import qualified Data.Heap as H
import Database.MongoDB

-- atime = last content access
-- mtime = content of $Data or $Index was last modified
-- ctime = metadata change last modify
-- crtime = create time
--CurrentFunctionality... basic timeline analysis

test = "MD5|name|inode|mode_as_string|UID|GID|size|atime|mtime|ctime|crtime"
data AccessType = ATime | MTime | CTime | CrTime
                  deriving (Show)
---perhaps compose AccessEvent of an Event, with tag..
--Data AccessEvent = A Event | M Event | .. etc...
-- I think below is a cheaper way.. and you remove a level of indirection
data AccessEvent = AE {
       fn :: B.ByteString,
       time   :: Integer,
       at     :: AccessType
     } deriving (Show)

instance Eq AccessEvent where
         a == b = (time a) == (time b)

--may improve this for 2nd-level ordering by name etc..
instance Ord AccessEvent where
         compare a b
           |(time a) < (time b) = LT
           |(time a) == (time b) = EQ
           |otherwise = GT



-- Basic Timelining stuff
--data HeapOrder = MAX | MIN
--type AEHeap = Either (H.MinHeap AccessEvent) (H.MaxHeap AccessEvent)
-- can do this to improve performance of max and increase flexibility
--performHeapOp func (Left min)   = Left (func min)
--performheapOp func (Right max)  = Right (func max)

type AEHeap = H.MinHeap AccessEvent

new_aeh :: AEHeap
new_aeh = H.empty

aeh_insert :: AccessEvent -> AEHeap -> AEHeap
aeh_insert = H.insert



basicTimeline :: [Event] -> [AccessEvent]
basicTimeline es = H.toAscList (heapify es)

heapify :: [Event] -> AEHeap
heapify es = foldr heapify' new_aeh es
   where
      heapify' :: Event -> AEHeap -> AEHeap
      heapify' e aeh = aeh4
         where
            aevent = AE (e_fn e) (e_atime e) ATime
            mevent = AE (e_fn e) (e_mtime e) MTime
            cevent = AE (e_fn e) (e_ctime e) CTime
            crevent = AE(e_fn e) (e_crtime e) CrTime
            aeh' = aeh_insert aevent aeh
            aeh2 = aeh_insert mevent aeh'
            aeh3 = aeh_insert cevent aeh2
            aeh4 = aeh_insert crevent aeh3

withinRange :: AEHeap -> Integer -> Integer -> AEHeap
withinRange aeh start finish = H.filter (\e -> (start <= (time e)) && ((time e) <= finish)) aeh

